

import 'dart:io';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_screenutil/screenutil_init.dart';
import 'package:hc_center/conf/ResConf.dart';
import 'package:hc_center/conf/StrConf.dart';
import 'package:hc_center/routes/routes.dart';
import 'package:permission_handler/permission_handler.dart';



class App extends StatefulWidget {
  App({Key key}) : super(key: key);

  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  ///路由
  final AbstractRoutes routes = Routes.routes;

  final ThemeData _lightTheme =
  ThemeData.light().copyWith(accentColor: Colors.transparent);
  final ThemeData _darkTheme =
  ThemeData.dark().copyWith(accentColor: Colors.transparent);

  Future _init() async {
    ///权限申请
    if (Platform.isAndroid)
       await Permission.storage.request().isGranted;
    ///初始化配置文件

    SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
      // 设置EasyRefresh的默认样式
      EasyRefresh.defaultHeader = ClassicalHeader(
        enableInfiniteRefresh: false,
        refreshText: '下拉刷新',
        refreshReadyText: '释放刷新',
        refreshingText: '刷新中...',
        refreshedText: '刷新完成',
        refreshFailedText:'刷新失败',
        noMoreText: '已无更多',
        infoText: '上次更新',
      );
      EasyRefresh.defaultFooter = ClassicalFooter(
        enableInfiniteLoad: true,
        loadText: '上拉加载',
        loadReadyText: '释放加载',
        loadingText: '加载中...',
        loadedText: '加载完成',
        loadFailedText: '加载失败',
        noMoreText: '已无更多',
        infoText: '上次加载',
      );
    });
  }

  @override
  void initState() {
    _init();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {

    return OrientationBuilder(
      builder: (_ctx, orientation){
        bool isLandscape = orientation == Orientation.landscape;
        return ScreenUtilInit(
          designSize: isLandscape ? Size(1334, 750): Size(750, 1334),
          allowFontScaling: false,
          builder: () => MaterialApp(
            title: StrConf.appName,
            debugShowCheckedModeBanner: false,
            theme: _lightTheme,
            darkTheme: _darkTheme,
            home: routes.buildPage(RoutesName.startPage, null),
            onGenerateRoute: (RouteSettings settings) {
              return MaterialPageRoute<Object>(builder: (BuildContext context) {
                return routes.buildPage(settings.name, settings.arguments);
              });
            },
          ),
        );
      },
    );
  }

}